package com.caishi.lkx.common.context;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.caishi.lkx.user.model.GroupModel;
import com.caishi.lkx.user.model.UserModel;
import com.caishi.lkx.user.service.IAdminUserService;
import com.caishi.lkx.user.service.IGroupService;
import com.caishi.lkx.user.service.IMemberService;
import com.caishi.lkx.user.service.IUserService;
import com.zzw.common.IUserContext;
import com.caishi.lkx.common.ienum.PlatformType;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Map;

/**
 * @author by keray
 * date:2020/6/26 10:35 上午
 */
@Configuration
@Slf4j
public class UserContext implements IUserContext<TokenData> {
    @Resource
    @Lazy
    private IUserService<UserModel> userService;
    @Resource
    @Lazy
    private IAdminUserService adminUserService;
    @Resource
    @Lazy
    private IGroupService groupService;
    @Resource
    @Lazy
    private IMemberService memberService;
    private final ThreadLocal<String> ipLocal = new InheritableThreadLocal<>();

    private final ThreadLocal<TokenData> tokenDataLocal = new InheritableThreadLocal<>();
    private final ThreadLocal<HttpServletRequest> currentRequestLocal = new InheritableThreadLocal<>();

    @Override
    public String currentUserId() {
        return currentTokenData().getUserId();
    }

    @Override
    public void setUserId(String userId) {

    }

    @Override
    public String currentIp() {
        return ipLocal.get();
    }

    @Override
    public void setIp(String ip) {
        ipLocal.set(ip);
    }

    @Override
    public HttpServletRequest currentRequest() {
        return currentRequestLocal.get();
    }

    @Override
    public TokenData currentTokenData() {
        TokenData data = tokenDataLocal.get();
        if (data == null) {
            setTokenData(new TokenData());
            return currentTokenData();
        }
        return data;
    }

    @Override
    public void setTokenData(TokenData tokenData) {
        tokenDataLocal.set(tokenData);
    }

    @Override
    public String getDuid() {
        return currentTokenData().getDeviceUUID();
    }

    @Override
    public void setDUid(String duid) {
        currentTokenData().setDeviceUUID(duid);
    }

    @Override
    public void setCurrentRequest(HttpServletRequest request) {
        currentRequestLocal.set(request);
    }


    public Long currentGroupId() {
        Long id = null;
        //if (currentTokenData().getGroupTime() != null && currentTokenData().getGroupTime().isAfter(LocalDateTime.now())) {
            if (!Long.valueOf(0).equals(currentTokenData().getGroupId())) {
                id = currentTokenData().getGroupId();
            }
        //}
        if (StrUtil.isBlank(currentUserId())) return null;
        return id;

    }

    private Long getMemberGroupId(UserModel userModel) {
        var member = memberService.getById(userModel.getId());
        if (null == member) return null;
        var groupId = member.getGroupId();
        if (groupId != null) {
            // 机构过期时间
            LocalDate time;
            GroupModel model = groupService.getById(groupId);
            if (null == model) return null;
            //time = model.getExpireTime();
//            if (null == time) {
//                return null;
//            }
//            if (time.atTime(23, 59, 59).isAfter(LocalDateTime.now())) {
//                return groupId;
//            }
        }
        return null;
    }

    public String currentZTUserKey() {
        return currentTokenData().getZtUserKey();
    }

    public Long currentLocalGroupId() {
        return currentTokenData().getLocalGroupId();
    }

    public PlatformType currentPlatform() {
        return currentTokenData().getPlatformType();
    }

    @Override
    public void importConf(Map<String, Object> map) {
        var tokenData = map.get("tokenData");
        if (tokenData instanceof Map ts) {
            map.put("tokenData", BeanUtil.mapToBean(ts, TokenData.class, true));
        }
        IUserContext.super.importConf(map);
    }

    public void clear() {
        IUserContext.super.clear();
        ipLocal.remove();
        currentRequestLocal.remove();
        tokenDataLocal.remove();
    }

}
